﻿using MCPlatform.MotionLib.Base.AssitantClass;
using MCPlatform.Shared.DAL.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MCPlatform.Shared.DAL.Contexts
{
    public class ProductionDataContext:DbContext
    {
        public DbSet<ProductionDataModel> ProductionDataEntities { get; set; }

        protected NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

        private string _connectionString = "Data Source = LAPTOP-314FP91O;Initial Catalog = ProductionData; Integrated Security = True;TrustServerCertificate=True;";

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(_connectionString);
        }


        /// <summary>
        /// 增加数据
        /// </summary>
        /// <param name="entity"></param>
        public void Add(ProductionDataModel entity)
        {
            try
            {
                logger.Info("Add Data To Db");
                this.ProductionDataEntities.Add(entity);
                this.SaveChanges();
            }
            catch (Exception e)
            {
                logger.Error("Add Data To Db",e);
                throw;
            }
        }


        /// <summary>
        /// 查询数据，第一条
        /// </summary>
        /// <returns></returns>
        public ProductionDataModel ReadFirst()
        {
            try
            {

                //using (SqlConnection connection = new SqlConnection(_connectionString))
                //{
                //    try
                //    {
                //        connection.Open();
                //        Console.WriteLine("连接成功！");
                //    }
                //    catch (Exception ex)
                //    {
                //        Console.WriteLine("连接失败： " + ex.Message);
                //    }
                //}


                logger.Info("Read First Data From Db");
                return this.ProductionDataEntities.OrderBy(b=>b.Id).First();
            }
            catch (Exception e)
            {
                logger.Error("Read First Data From Db", e);
                throw;
            }
        }


        /// <summary>
        /// 查询数据最后一条
        /// </summary>
        /// <returns></returns>

        public ProductionDataModel ReadLast()
        {
            try
            {
                logger.Info("Read Last Data To Db");
                return this.ProductionDataEntities.OrderBy(b => b.Id).Last();
            }
            catch (Exception e)
            {
                logger.Error("Read Last Data From Db", e);
                throw;
            }
        }


        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="newEntity"></param>
        public void Update(ProductionDataModel newEntity)
        {
            try
            {
                logger.Info("Update Data To Db");
                var entity = this.ProductionDataEntities.AsEnumerable().FirstOrDefault(e => (newEntity.StartTime- e.StartTime) < TimeSpan.FromSeconds(5));
                if (entity != null)
                {
                    newEntity.Id = entity.Id;
                    Common.AutoMapProperties(newEntity,entity);
                    this.SaveChanges();
                }
                else
                {
                    //没有就新增
                    Add(newEntity);
                }
            }
            catch (Exception e)
            {
                logger.Error("Update Data To Db", e);
                throw;
            }
        }


        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="deleteEntity"></param>
        public void Delete(ProductionDataModel deleteEntity)
        {
            try
            {
                logger.Info("Delete Data From Db");
                var entity = this.ProductionDataEntities.FirstOrDefault(e => e.Id == deleteEntity.Id);
                if (entity != null)
                {
                    this.ProductionDataEntities.Remove(entity);
                    this.SaveChanges();
                }
            }
            catch (Exception e)
            {
                logger.Error("Delete Data From Db", e);
                throw;
            }
        }


    }
}
